//
//  HWGameTestCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/6/21.
//  Copyright © 2022 94hwan. All rights reserved.
//

import UIKit

class HWGameTestCell: UITableViewCell {
    var bgView:UIView!
    var gameImageView:UIImageView!
    var titleLabel:UILabel!
    var contentLabel:UILabel!
    var bottomLabel:UILabel!
    var doneStatus:UILabel!
    var lineView:UIView!
    var bottomLeftLabel:UILabel!
    var rewardLabel:UILabel!
    var rightButton:UIButton!
    var rightButtonClick:((_:HWGameTestModel)->())?
    var model:HWGameTestModel?{
        didSet{
            gameImageView.kf.setImage(with: URL(string: model?.icon ?? ""))
            titleLabel.text = model?.name
            contentLabel.text = model?.content
            bottomLabel.text = "#\(model?.end_time ?? "")结束"
            doneStatus.text = "参与：\(model?.ok ?? "")/\(model?.num ?? "")"
            rewardLabel.text = model?.jiangli
            rightButton.isEnabled = true
            rightButton.isHidden = false
            if model?.status == "0" {
                rightButton.setTitle("报名", for: .normal)
                rightButton.setTitleColor(.colorWithHexString(hexString: "#FF7D1F"), for: .normal)
                rightButton.layer.borderColor = UIColor.colorWithHexString(hexString: "#FF7D1F").cgColor
                
            } else if model?.status == "1" {
                rightButton.setTitle("进行中", for: .normal)
                rightButton.setTitleColor(.colorWithHexString(hexString: "#CCCCCC"), for: .normal)
                rightButton.layer.borderColor = UIColor.colorWithHexString(hexString: "#CCCCCC").cgColor
            } else if model?.status == "2" {
                rightButton.setTitle("领奖", for: .normal)
                rightButton.setTitleColor(.colorWithHexString(hexString: "#29C571"), for: .normal)
                rightButton.layer.borderColor = UIColor.colorWithHexString(hexString: "#29C571").cgColor
            } else if model?.status == "3" {
                rightButton.setTitle("已领取", for: .normal)
                rightButton.setTitleColor(.colorWithHexString(hexString: "#CCCCCC"), for: .normal)
                rightButton.layer.borderColor = UIColor.colorWithHexString(hexString: "#CCCCCC").cgColor
                rightButton.isEnabled = false
            }else {
                rightButton.isEnabled = false
                rightButton.isHidden = true
            }
        }
    }
    class func cellWithTableView(tableView:UITableView) -> (HWGameTestCell) {
        let reUse:String = "HWGameTestCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWGameTestCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWGameTestCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        bgView.layer.shadowColor = UIColor.colorWithAlphaHexString(hexString: "#E7E7E7", alpha: 1).cgColor
        bgView.layer.shadowOffset = CGSize(width: 0, height: 0)
        bgView.layer.shadowRadius = 9*widthScale
        bgView.layer.shadowOpacity = 1
        bgView.layer.cornerRadius = 7*widthScale
    }
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        bgView = UIView()
        bgView.backgroundColor = .white
        contentView.addSubview(bgView)
        
        gameImageView = UIImageView()
        gameImageView.contentMode = .scaleAspectFill
        gameImageView.layer.masksToBounds = true
        gameImageView.layer.cornerRadius = 12*widthScale
        bgView.addSubview(gameImageView)
        
        titleLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 14*widthScale)
        titleLabel.numberOfLines = 1
        bgView.addSubview(titleLabel)
        
        contentLabel = HWLabel.createLabel(title: "", color: "#888888", font: 11*widthScale)
        contentLabel.numberOfLines = 1
        bgView.addSubview(contentLabel)
        
        bottomLabel = HWLabel.createLabel(title: "", color: "#FD9F65", font: 11*widthScale)
        bottomLabel.numberOfLines = 1
        bgView.addSubview(bottomLabel)
        
        doneStatus = HWLabel.createLabel(title: "", color: "#888888", font: 11*widthScale)
        bgView.addSubview(doneStatus)
        
        lineView = UIView()
        lineView.frame = CGRect(x: 0, y: 0, width: 321*widthScale, height: 1*widthScale)
        bgView.addSubview(lineView)
        
        let shapeLayer:CAShapeLayer = CAShapeLayer()
        
        shapeLayer.bounds = lineView.bounds
        
        shapeLayer.position = CGPoint(x: lineView.frame.width / 2, y: lineView.frame.height / 2)
        
        shapeLayer.fillColor = UIColor.clear.cgColor
        shapeLayer.strokeColor = UIColor.colorWithAlphaHexString(hexString: "#979797", alpha: 0.5).cgColor
        
        shapeLayer.lineWidth = 1*widthScale
        shapeLayer.lineJoin = .round
        shapeLayer.lineDashPhase = 0
        shapeLayer.lineDashPattern = [NSNumber(value: 1*widthScale), NSNumber(value: 1*widthScale)]
        
        let path:CGMutablePath = CGMutablePath()
        path.move(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x: lineView.frame.width, y: 0))
        shapeLayer.path = path
        
        lineView.layer.addSublayer(shapeLayer)
        
        bottomLeftLabel = HWLabel.createLabel(title: "任务奖励：", color: "#888888", font: 11*widthScale)
        bgView.addSubview(bottomLeftLabel)
        
        rewardLabel = HWLabel.createLabel(title: "", color: "#FF726A", font: 11*widthScale)
        bgView.addSubview(rewardLabel)
        
        rightButton = UIButton(type: .custom)
        rightButton.layer.cornerRadius = 14*widthScale
        rightButton.layer.borderWidth = 1
        rightButton.titleLabel?.font = .systemFont(ofSize: 13*widthScale ,weight: .semibold)
        rightButton.addTarget(self, action: #selector(rightButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(rightButton)
    }
    
    func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.right.equalTo(-19)
            make.top.equalTo(9*widthScale).priority(.high)
            make.bottom.equalToSuperview()
        }
        
        gameImageView.snp.makeConstraints { make in
            make.left.top.equalTo(9*widthScale)
            make.width.height.equalTo(63*widthScale)
            make.bottom.equalTo(-50*widthScale)
        }
        
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(gameImageView.snp.right).offset(10*widthScale)
            make.top.equalTo(gameImageView.snp.top).offset(2*widthScale)
            make.right.lessThanOrEqualTo(doneStatus.snp.left).offset(-10*widthScale)
            make.height.equalTo(20*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(5*widthScale)
            make.right.equalTo(-15*widthScale)
            make.height.equalTo(15*widthScale)
        }
        
        bottomLabel.snp.makeConstraints { make in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(5*widthScale)
            make.right.equalTo(-15*widthScale)
            make.height.equalTo(15*widthScale)
        }
        
        doneStatus.snp.makeConstraints { make in
            make.right.equalTo(-9*widthScale)
            make.top.equalTo(13*widthScale)
            make.height.equalTo(15*widthScale)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.equalTo(9*widthScale)
            make.top.equalTo(gameImageView.snp.bottom).offset(8*widthScale)
            make.height.equalTo(1)
            make.right.equalTo(-9*widthScale)
        }
        
        bottomLeftLabel.snp.makeConstraints { make in
            make.left.equalTo(9*widthScale)
            make.top.equalTo(lineView.snp.bottom).offset(13*widthScale)
            make.height.equalTo(15*widthScale)
        }
        
        rewardLabel.snp.makeConstraints { make in
            make.centerY.equalTo(bottomLeftLabel)
            make.left.equalTo(bottomLeftLabel.snp.right)
        }
        
        rightButton.snp.makeConstraints { make in
            make.right.equalTo(-9*widthScale)
            make.top.equalTo(lineView.snp.bottom).offset(6*widthScale)
            make.height.equalTo(28*widthScale)
            make.width.equalTo(54*widthScale)
        }
    }
    
    @objc func rightButtonClick(_:UIButton) -> Void {
        self.rightButtonClick?(model!)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
